//
//  JKRFeedDetailCommentCountView.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/9.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedDetailCommentCountView.h"

@interface JKRFeedDetailCommentCountView ()

@property (nonatomic, strong) YYLabel *commentCountLabel;
@property (nonatomic, strong) YYLabel *voteUserLabel;
@property (nonatomic, strong) YYLabel *voteNumLabel;
@property (nonatomic, strong) UIView *bottomLine;

@end

@implementation JKRFeedDetailCommentCountView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor whiteColor];
    @weakify(self);
    _commentCountLabel = [self _creatYYLabel];
    [self addSubview:_commentCountLabel];
    _voteUserLabel = [self _creatYYLabel];
    _voteUserLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weak_self highLightClickWithText:text range:range];
    };
    [self addSubview:_voteUserLabel];
    _voteNumLabel = [self _creatYYLabel];
    _voteNumLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weak_self highLightClickWithText:text range:range];
    };
    [self addSubview:_voteNumLabel];
    _bottomLine = [UIView new];
    _bottomLine.backgroundColor = UIColorHex(E1E1E1);
    [self addSubview:_bottomLine];
    
    return self;
}

- (void)setLayout:(JKRFeedCommentCountLayout *)layout {
    _layout = layout;
    self.voteUserLabel.frame = layout.voteUserLabelFrame;
    self.voteUserLabel.textLayout = layout.voteUserLabelLayout;
    self.voteNumLabel.frame = layout.voteNumLabelFrame;
    self.voteNumLabel.textLayout = layout.voteNumLabelLayout;
    self.bottomLine.frame = layout.bottomLineFrame;
    
    self.commentCountLabel.frame = layout.commentCountLabelFrame;
    self.commentCountLabel.textLayout = layout.commentCountLabelLayout;
}

- (void)highLightClickWithText:(NSAttributedString *)text range:(NSRange)range {
    YYTextHighlight *highlight = [text attribute:YYTextHighlightAttributeName atIndex:range.location];
    NSDictionary *info = highlight.userInfo;
    if (info.count == 0) {
        return;
    }
    
    if (info[kFeedCommentVoteTapUser]) {
        if ([info[kFeedCommentVoteTapUser] isEqualToString:kFeedCommentVoteTapMoreUserID] && self.delegate && [self.delegate respondsToSelector:@selector(feedCommentCountViewDidClickVoteUserList)]) {
            [self.delegate feedCommentCountViewDidClickVoteUserList];
            return;
        }
        if (self.delegate && [self.delegate respondsToSelector:@selector(feedCommentCountViewDidClickUserWithUserID:)]) {
            [self.delegate feedCommentCountViewDidClickUserWithUserID:info[kFeedCommentVoteTapUser]];
        }
        return;
    }
}

- (YYLabel *)_creatYYLabel {
    YYLabel *label = [YYLabel new];
    label.ignoreCommonProperties = YES;
    label.fadeOnAsynchronouslyDisplay = YES;
    label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    return label;
}

@end
